package com.hytc.mymall.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hytc.mymall.R;
import com.hytc.mymall.adapter.SelectPopAdapter;
import com.hytc.mymall.utils.BaseTools;

import java.util.List;

/**
 * Created by Administrator on 2016/12/1.
 */

public class SelectPop {
    private Activity activity;
    private InputMethodManager imm;
    private ImageView iv;
    private List<String> list;
    private ListView lv;
    private PopupWindow pop;
    private TextView tv;

    public SelectPop(final Activity activity, final List<String> list, final ImageView iv, final TextView tv, final boolean b, final SelectPopCallback selectPopCallback) {
        this.activity = activity;
        this.iv = iv;
        this.tv = tv;
        this.list = list;
        final View inflate = LayoutInflater.from((Context)activity).inflate(R.layout.pop_select, (ViewGroup)null);
        this.imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        int windowsWidth;
        if (b) {
            windowsWidth = BaseTools.getWindowsWidth(activity);
        }
        else {
            windowsWidth = 1 * BaseTools.getWindowsWidth(activity) / 4;
        }
        int n;
        if (list.size() <= 5) {
            n = BaseTools.dip2px((Context)activity, 50 * list.size());
        }
        else {
            n = BaseTools.dip2px((Context)activity, 300.0f);
        }
        (this.pop = new PopupWindow(inflate, windowsWidth, n, true)).setOnDismissListener((PopupWindow.OnDismissListener)new PopupWindow.OnDismissListener() {
            public void onDismiss() {
                iv.startAnimation(AnimationUtils.loadAnimation((Context)activity, R.anim.pop_enter));
            }
        });
        this.lv = (ListView)inflate.findViewById(R.id.lv);
        if (b) {
            this.lv.setBackgroundResource(R.drawable.bg_list_select);
            this.lv.setDividerHeight(1);
            this.lv.setDivider((Drawable)new ColorDrawable(activity.getResources().getColor(R.color.red)));
        }
        else {
            this.lv.setBackgroundResource(R.drawable.selectpop_bg2);
        }
        this.lv.setFocusable(true);
        this.lv.setAdapter((ListAdapter)new SelectPopAdapter((Context)activity, list, b));
        this.lv.setOnItemClickListener((AdapterView.OnItemClickListener)new AdapterView.OnItemClickListener() {
            public void onItemClick(final AdapterView<?> adapterView, final View view, final int n, final long n2) {
                SelectPop.this.pop.dismiss();
                if (b) {
                    tv.setText((CharSequence)list.get(n));
                }
                selectPopCallback.onItemClick(n);
            }
        });
    }

    public void Show(final Handler handler, final int n, final EditText softKeyboardShow) {
        this.setSoftKeyboardShow(softKeyboardShow);
        new Thread() {
            @Override
            public void run() {
                final Message obtain = Message.obtain();
                obtain.what = n;
                handler.sendMessageDelayed(obtain, 100L);
            }
        }.start();
    }

    public void setSoftKeyboardShow(final EditText editText) {
        if (this.imm.isActive()) {
            this.imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }

    public void setselection(final int n) {
        this.tv.setText((CharSequence)this.list.get(n));
    }

    public void showPop() {
        this.iv.startAnimation(AnimationUtils.loadAnimation((Context)this.activity, R.anim.push_bottom_in));
        this.pop.setBackgroundDrawable((Drawable)new ColorDrawable(0));
        this.pop.showAsDropDown((View)this.tv, 0, 0);
        this.pop.setAnimationStyle(2131492965);
        this.pop.setFocusable(true);
        this.pop.setOutsideTouchable(false);
        this.pop.setInputMethodMode(1);
        this.pop.setSoftInputMode(16);
        this.pop.update();
    }
}

